//
//  ArticleView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/11.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol ArticleViewDelegate {
    @objc optional func touchLoad(title:String) //点击展开
    @objc optional func selectCardType(title:String) //选择信用卡类型
}

class ArticleView: UIView {
    
    weak var articleDelegate : ArticleViewDelegate?
    
    fileprivate let cellId = "articleCell"
    
    fileprivate let cardArray = [pinCard,late,bill,activated]
    fileprivate let moreCardArray = [pinCard,late,bill,activated,annualFee,blacklist,handlingFee,interest,changeCard]
    
    fileprivate var dataArray = [String]()
    
    var collectionView : UICollectionView!
    
    fileprivate let titleLabel = UILabel()
    
    fileprivate let touchButton = UIButton()
    fileprivate let imageView = UIImageView()
    
    var text = "" {
        didSet{
            collectionView.reloadData()
        }
    }

    init() {
        super.init(frame: .zero)
        
        self.dataArray = cardArray
        
        self.backgroundColor = UIColor.white
        
        titleLabel.text = moreArticle
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 18))
        self.addSubview(titleLabel)
        
        let grayView = UIView()
        grayView.backgroundColor = UIColor.lineColor()
        self.addSubview(grayView)
        
        touchButton.setTitle(touchMore, for: .normal)
        touchButton.setTitleColor(UIColor.gray, for: .normal)
        touchButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        touchButton.addTarget(self, action: #selector(touchLoad(sender:)), for: .touchUpInside)
        self.addSubview(touchButton)
        
        imageView.image = #imageLiteral(resourceName: "double_arrow_bottom")
        self.addSubview(imageView)
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize.init(width: ScreenW/2, height: RATIO_H(maxNum: 40))
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.isScrollEnabled = false
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TypeCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 8))
            make.right.equalToSuperview().offset(RATIO(maxNum: -8))
            make.top.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        grayView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(titleLabel.snp.bottom)
            make.height.equalTo(1)
        }
        
        touchButton.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.centerX.equalToSuperview().offset(RATIO(maxNum: -11))
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        imageView.snp.makeConstraints { (make) in
            make.left.equalTo(touchButton.snp.right).offset(RATIO(maxNum: 2))
            make.centerY.equalTo(touchButton)
            make.width.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(touchButton.snp.top)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 点击加载
    @objc fileprivate func touchLoad(sender:UIButton) {
        
        if sender.titleLabel?.text == touchMore {
            sender.setTitle(touchLess, for: .normal)
            imageView.image = #imageLiteral(resourceName: "double_arrow_top")
            self.dataArray = moreCardArray
            self.collectionView.reloadData()
        }else{
            sender.setTitle(touchMore, for: .normal)
            imageView.image = #imageLiteral(resourceName: "double_arrow_bottom")
            self.dataArray = cardArray
            self.collectionView.reloadData()
        }
        
        self.articleDelegate?.touchLoad!(title: (sender.titleLabel?.text)!)
    }

}

extension ArticleView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? TypeCell
        cell?.titleLabel.text = self.text + creditCard + dataArray[indexPath.row]
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.articleDelegate?.selectCardType!(title: dataArray[indexPath.row])
        
    }
    
}
